<?php
/*
 * 订单退款控制器
 */
namespace Mobile\Controller;

use Mobile\Model\orderModel;
use Mobile\Model\tmpmsgModel;

class OrdertuikuanController extends BaseController {

    public $orderModel;
    public $tmpmsgModel;
    
    public function __construct() {
        parent::__construct();
        $this->orderModel = new orderModel();
        $this->tmpmsgModel = new tmpmsgModel();
    }
    
 

    /*
     * 退款ajax
     */
    public function index(){
        $order_id = get("order_id");
        //判断这个订单是不是我的
        $where = array(
            "order_id" => $order_id,
            "uid" => $this->uid
        );
        $orderinfo = $this->orderModel->getOrder($where);
        if(empty($orderinfo)){
            return_json(1);
        }
    
        if($orderinfo['checked'] == 1){
            //可以确认收货
            $data = array(
                "checked" => 3
            );
            $res = $this->orderModel->editOrder($where, $data);
    
            if($res){
                //写扣除佣金流水记录放到admin执行
                //发送秒板消息退货
                // 退货申请提醒
                $this->_tuihuoMsg($orderinfo);
                return_json(0,"已提交退款申请,请等待商家审核","","/mobile/orderdetail?order_id=".$order_id);
            }
        }
        return_json(1,"当前状态不能退款");
    }
    
    /*
     * 取消退款ajax
     */
    public function cancel(){
        $order_id = get("order_id");
        //判断这个订单是不是我的
        $where = array(
            "order_id" => $order_id,
            "uid" => $this->uid
        );
        $orderinfo = $this->orderModel->getOrder($where);
        if(empty($orderinfo)){
            return_json(1);
        }
    
        if($orderinfo['checked'] == 3){
            //可以取消退款
            $data = array(
                "checked" => 1
            );
            $res = $this->orderModel->editOrder($where, $data);
            if($res){
                return_json(0,"已取消退款申请~","","/mobile/orderdetail?order_id=".$order_id);
            }
        }
    
        return_json(1,"当前状态不能取消退款");
    }
    
    
    /*
     * 拼接退货申请模板
     *                 {{first.DATA}}
     订单编号：{{keyword1.DATA}}
     商品名称：{{keyword2.DATA}}
     订单金额：{{keyword3.DATA}}
     {{remark.DATA}}
     */
    protected function _tuihuoTmp($order){
    
        $tmp_id = $this->tmpl['TUIHUO_APPLY']['tmp_id'];
        $color = $this->tmpl['TUIHUO_APPLY']['color'];
    
        $url = get_domain()."/mobile/orderlists/?status=3";
        $content = "你提交了退货申请,请等待管理员审核";
        $footer ='\\n'."点击查看详情...";
        $msg = array(
            "touser" => $order['openid'],
            "template_id" => $tmp_id,//
            'url' => $url,
            "topcolor" => $color,
            'data' => array(
                'first' => array(
                    'value' => urlencode($content.'\\n'),
                    'color' => $color
                ),
                'keyword1' => array(
                    'value' => urlencode($order['order_sn']),//订单号
                    'color' => $color,
                ),
                'keyword2' => array(
                    'value' => urlencode($order['order_title']),//商品名称
                    'color' => $color,
                ),
                'keyword3' => array(
                    'value' => urlencode($order['pay_money']),//订单金额
                    'color' => $color,
                ),
                'remark' => array(
                    'value' => urlencode($footer),
                    'color' => $color,
                ),
            ),
        );
    
        return $msg = urldecode(json_encode($msg));
    }
    
    
    /*
     * 退货提醒
     */
    protected function _tuihuoMsg($order){
        //判断是否关注, 没关注就别发了,TODO
    
        //发送模板消息
        $msg = $this->_tuihuoTmp($order);//拼接模板消息
    
        $msgttt = array(
            "uid" => $order['uid'],
            "openid" => $order['openid'],
            "nickname" => $order['nickname'],
            "groupid" => -1,
            "content" => $msg,
            "send_priority" => 1,
            "create_time" => time(),
            "send_time" => time(),
            "status" => -1,
            "send_plan_time" => time()
        );
        
        $this->tmpmsgModel->addtmpmsg($msgttt);//添加消息到数据库队列
    
        return true;
    }
    
    
    
}